rm(list = ls())
setwd("./Fig.3C_F")


library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(openxlsx)

enrich_circle_data<-read.xlsx("All_GOanalysis.xlsx", sheet=1,check.names = F, sep = "\t")


View(enrich_circle_data)
dat <- enrich_circle_data #[,c(1:4)]
colnames(dat)
colnames(dat)[2] <- 'group'
dat$'-log10p' <- -log10(dat$pvalue)

dat <- dat %>% filter(row_number() <= 25)

dat$RichFactor <- (dat$up + dat$down) / dat$all
dat$group <- as.factor(dat$group) 

table(dat$group)

dat <- dat %>% dplyr::group_by(group)  %>% arrange(desc(RichFactor)) %>% arrange(group) 

dat$id <- seq(1, nrow(dat))
# # # # Build label_data ----
# Get the position and inclination angle of the name of each sample on the Y axis.
label_data <- dat#[,c('ID', 'id')]
number_of_bar <- nrow(label_data) 
angle <- 90 - 360 * (label_data$id-0.5) / number_of_bar 
label_data$hjust <- ifelse(angle < -90 , 1, 0) 
label_data$angle <- ifelse(angle > 0 | angle < -180 , angle-90, angle+90) 
label_data$angle_vertical <- ifelse(angle < -90 , angle+180, angle) 
label_data$start <- label_data$id - 0.45
label_data <- label_data %>% 
  mutate(end=start + 0.9, all_gene=up_gene+down_gene) %>% 
  # mutate(deg_background_gene_ratio = (up_gene/all_gene)) %>% 
  mutate(gene_length = log10(all)) %>% 
  mutate(up_down_gene_ratio_end = start + (0.9*up_gene/all_gene), gene_length_end = start + 0.9 * (6/7) *(gene_length/3)) 



range_sep_num <- range(label_data$'-log10p') %>% round(0)
separate_num <- seq(range_sep_num[1],range_sep_num[2],0.5)
label_data$colour_pvalue <- cut(label_data$'-log10p', breaks = c(-Inf, separate_num, Inf), labels = c(separate_num[1]-0.5, separate_num), right=FALSE)
fill_colour = c("#F7D116", "deepskyblue","olivedrab2") 
label_data <- table(label_data$group) %>% 
  as.data.frame() %>% 
  dplyr::rename(group=Var1) %>% 
  mutate(fill_colour = fill_colour ) %>% 
  left_join(label_data, ., "group") 


p1 <-
  ggplot(label_data)+

  geom_bar(aes(x=as.factor(id), y= 1 ), stat="identity", 
           alpha=0.2)  +

  geom_bar(aes(x=as.factor(id), y= RichFactor*18 , fill=group), stat="identity", 
           alpha=0.8)  + guides(fill=guide_legend(title=NULL)) + 

  geom_segment(data= label_data, aes(x = start, y = 1.42, xend =  end, yend = 1.42), 
               colour = label_data$fill_colour, alpha=1, linewidth=4 ,inherit.aes = FALSE) +  
  geom_segment(data= label_data, aes(x = start, y = 1.3, xend =  gene_length_end, yend = 1.3, colour = as.numeric(colour_pvalue)), alpha=1, size=3 ,inherit.aes = FALSE) + 
  guides(colour=guide_legend(title='-log10(pvalue)')) + 
  geom_segment(data= label_data, aes(x = start, y = 1.2, xend =  end, yend = 1.2),
               colour = "#8E96CC", alpha=1, size=3 ,inherit.aes = FALSE) +  
  geom_segment(data= label_data, aes(x = start, y = 1.2, xend =  up_down_gene_ratio_end, yend = 1.2),
               colour = "#740F67", alpha=1, size=3 ,inherit.aes = FALSE) +  
  geom_blank(aes(y = -0.5)) +
  theme_minimal() +
  theme(#legend.position = "none",
    axis.text = element_blank(),
    axis.title = element_blank(), 
    panel.grid = element_blank(), 
    # plot.margin = unit(rep(-1,5), "cm") 
  )+ 
  coord_polar(theta = "x", start = 0, direction=1) +
  scale_fill_manual(values = fill_colour) +
  scale_color_steps(low = "#FEE8DE", high = "red", breaks = c(-Inf, separate_num, Inf))
p1



p2 <-p1+  geom_text(data=label_data, 
               aes(x=(start+end)/2, y=1.42, label=ID, hjust=0.5),
               color="black",fontface="bold",alpha=0.8, size=2.5, 
               angle= label_data$angle, inherit.aes = T) + 
  geom_text(data=label_data, 
            aes(x=(start+gene_length_end)/2, y=1.3, label=all, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) +
  geom_text(data=label_data, 
            aes(x=(start+up_down_gene_ratio_end)/2, y=1.1, label=up_gene, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) +
  geom_text(data=label_data, 
            aes(x=(up_down_gene_ratio_end+end)/2, y=1.1, label=down_gene, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) 
p2

p3 <- p2 + 
  geom_segment(data= grid_data, aes(x = start, y = grid_ylim, xend =  start, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = sec_breakpoint, y = grid_ylim, xend =  sec_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = third_breakpoint, y = grid_ylim, xend =  third_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = fourth_breakpoint, y = grid_ylim, xend =  fourth_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE)   
  # geom_segment(data= grid_data, aes(x = start, y = grid_ylim, xend =  fourth_breakpoint, yend = grid_ylim), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) 
p3

grid_data <- grid_data %>% 
  mutate(start_text = '1') %>% 
  mutate(sec_breakpoint_text = '10') %>% 
  mutate(third_breakpoint_text = '100') %>% 
  mutate(fourth_breakpoint_text = '1000') 
grid_ylimax_text <- grid_ylimax + 0.05
text_factor <- c('start_text','sec_breakpoint_text','third_breakpoint_text','fourth_breakpoint_text')
p4 <-
p3 +
  geom_text(data=grid_data, 
            aes(x=start, y=grid_ylimax_text, label=start_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T) +
  geom_text(data=grid_data, 
            aes(x=sec_breakpoint, y=grid_ylimax_text, label=sec_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T)  +
  geom_text(data=grid_data, 
            aes(x=third_breakpoint, y=grid_ylimax_text, label=third_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T)  +
  geom_text(data=grid_data, 
            aes(x=fourth_breakpoint, y=grid_ylimax_text, label=fourth_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T) 
p4
ggsave("p1.svg",p4)



################Fig.2:sheet2********************
enrich_circle_data<-read.xlsx("All_GOanalysis.xlsx", sheet=2,check.names = F, sep = "\t")


View(enrich_circle_data)
dat <- enrich_circle_data #[,c(1:4)]
colnames(dat)
colnames(dat)[2] <- 'group'
dat$'-log10p' <- -log10(dat$pvalue)

dat <- dat %>% filter(row_number() <= 25)



dat$RichFactor <- (dat$up + dat$down) / dat$all
dat$group <- as.factor(dat$group)
table(dat$group)

dat <- dat %>% dplyr::group_by(group)  %>% arrange(desc(RichFactor)) %>% arrange(group) 
dat$id <- seq(1, nrow(dat))

label_data <- dat#[,c('ID', 'id')]
number_of_bar <- nrow(label_data) 
angle <- 90 - 360 * (label_data$id-0.5) / number_of_bar 
label_data$hjust <- ifelse(angle < -90 , 1, 0)
label_data$angle <- ifelse(angle > 0 | angle < -180 , angle-90, angle+90) 
label_data$angle_vertical <- ifelse(angle < -90 , angle+180, angle) 
label_data$start <- label_data$id - 0.45
label_data <- label_data %>% 
  mutate(end=start + 0.9, all_gene=up_gene+down_gene) %>% 
  # mutate(deg_background_gene_ratio = (up_gene/all_gene)) %>% 
  mutate(gene_length = log10(all)) %>% 
  mutate(up_down_gene_ratio_end = start + (0.9*up_gene/all_gene), gene_length_end = start + 0.9 * (6/7) *(gene_length/3))  


range_sep_num <- range(label_data$'-log10p') %>% round(0)
separate_num <- seq(range_sep_num[1],range_sep_num[2],0.5)
label_data$colour_pvalue <- cut(label_data$'-log10p', breaks = c(-Inf, separate_num, Inf), labels = c(separate_num[1]-0.5, separate_num), right=FALSE)
fill_colour = c("#F7D116", "olivedrab2") 
label_data <- table(label_data$group) %>% 
  as.data.frame() %>% 
  dplyr::rename(group=Var1) %>% 
  mutate(fill_colour = fill_colour ) %>% 
  left_join(label_data, ., "group") 


p1 <-
  ggplot(label_data)+

  geom_bar(aes(x=as.factor(id), y= 1 ), stat="identity", 
           alpha=0.2)  +
  
  geom_bar(aes(x=as.factor(id), y= RichFactor*18 , fill=group), stat="identity", 
           alpha=0.8)  + guides(fill=guide_legend(title=NULL)) + 

  geom_segment(data= label_data, aes(x = start, y = 1.42, xend =  end, yend = 1.42), 
               colour = label_data$fill_colour, alpha=1, linewidth=4 ,inherit.aes = FALSE) +  
  geom_segment(data= label_data, aes(x = start, y = 1.3, xend =  gene_length_end, yend = 1.3, colour = as.numeric(colour_pvalue)), alpha=1, size=3 ,inherit.aes = FALSE) + 
  guides(colour=guide_legend(title='-log10(pvalue)')) + 
  geom_segment(data= label_data, aes(x = start, y = 1.2, xend =  end, yend = 1.2),
               colour = "#8E96CC", alpha=1, size=3 ,inherit.aes = FALSE) +  
  geom_segment(data= label_data, aes(x = start, y = 1.2, xend =  up_down_gene_ratio_end, yend = 1.2),
               colour = "#740F67", alpha=1, size=3 ,inherit.aes = FALSE) +  
  geom_blank(aes(y = -0.5)) +

  theme_minimal() +
  theme(#legend.position = "none", 
    axis.text = element_blank(),
    axis.title = element_blank(), 
    panel.grid = element_blank(), 
    # plot.margin = unit(rep(-1,5), "cm") 
  )+ 
  coord_polar(theta = "x", start = 0, direction=1) +
  scale_fill_manual(values = fill_colour) +
  scale_color_steps(low = "#FEE8DE", high = "red", breaks = c(-Inf, separate_num, Inf))
p1



p2 <-p1+  geom_text(data=label_data, 
                 aes(x=(start+end)/2, y=1.42, label=ID, hjust=0.5),
                 color="black",fontface="bold",alpha=0.8, size=2.5, 
                 angle= label_data$angle, inherit.aes = T) + 
  geom_text(data=label_data, 
            aes(x=(start+gene_length_end)/2, y=1.3, label=all, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) +
  geom_text(data=label_data, 
            aes(x=(start+up_down_gene_ratio_end)/2, y=1.1, label=up_gene, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) +
  geom_text(data=label_data, 
            aes(x=(up_down_gene_ratio_end+end)/2, y=1.1, label=down_gene, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) 
p2

grid_data <- label_data[,c('ID', 'end', 'start','hjust'), drop=F]
grid_data <- grid_data %>% 
  mutate(distance = end - start) %>% 
  mutate(sec_breakpoint = start + (2/7)*distance) %>% 
  mutate(third_breakpoint = start + (4/7)*distance) %>% 
  mutate(fourth_breakpoint = start + (6/7)*distance) 
grid_ylim <- 1.47
grid_ylimax <- grid_ylim + 0.05
p3 <- p2 + 
  geom_segment(data= grid_data, aes(x = start, y = grid_ylim, xend =  start, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = sec_breakpoint, y = grid_ylim, xend =  sec_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = third_breakpoint, y = grid_ylim, xend =  third_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = fourth_breakpoint, y = grid_ylim, xend =  fourth_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE)   
# geom_segment(data= grid_data, aes(x = start, y = grid_ylim, xend =  fourth_breakpoint, yend = grid_ylim), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) 
p3


grid_data <- grid_data %>% 
  mutate(start_text = '1') %>% 
  mutate(sec_breakpoint_text = '10') %>% 
  mutate(third_breakpoint_text = '100') %>% 
  mutate(fourth_breakpoint_text = '1000') 
grid_ylimax_text <- grid_ylimax + 0.05
text_factor <- c('start_text','sec_breakpoint_text','third_breakpoint_text','fourth_breakpoint_text')
p4 <-
  p3 +
  geom_text(data=grid_data, 
            aes(x=start, y=grid_ylimax_text, label=start_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T) +
  geom_text(data=grid_data, 
            aes(x=sec_breakpoint, y=grid_ylimax_text, label=sec_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T)  +
  geom_text(data=grid_data, 
            aes(x=third_breakpoint, y=grid_ylimax_text, label=third_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T)  +
  geom_text(data=grid_data, 
            aes(x=fourth_breakpoint, y=grid_ylimax_text, label=fourth_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T) 
p4
ggsave("p2.svg",p4)

#################fig.3:sheet3#################
enrich_circle_data<-read.xlsx("All_GOanalysis.xlsx", sheet=3,check.names = F, sep = "\t")

dat <- enrich_circle_data #[,c(1:4)]
colnames(dat)
colnames(dat)[2] <- 'group'
dat$'-log10p' <- -log10(dat$pvalue)

dat <- dat %>% filter(row_number() <= 25)


dat$RichFactor <- (dat$up + dat$down) / dat$all
dat$group <- as.factor(dat$group) 
table(dat$group)

dat <- dat %>% dplyr::group_by(group)  %>% arrange(desc(RichFactor)) %>% arrange(group) 
dat$id <- seq(1, nrow(dat))


label_data <- dat#[,c('ID', 'id')]
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) / number_of_bar
label_data$hjust <- ifelse(angle < -90 , 1, 0) 
label_data$angle <- ifelse(angle > 0 | angle < -180 , angle-90, angle+90) 
label_data$angle_vertical <- ifelse(angle < -90 , angle+180, angle) 
label_data$start <- label_data$id - 0.45
label_data <- label_data %>% 
  mutate(end=start + 0.9, all_gene=up_gene+down_gene) %>% 

  mutate(gene_length = log10(all)) %>% 
  mutate(up_down_gene_ratio_end = start + (0.9*up_gene/all_gene), gene_length_end = start + 0.9 * (6/7) *(gene_length/3)) 



range_sep_num <- range(label_data$'-log10p') %>% round(0)
separate_num <- seq(range_sep_num[1],range_sep_num[2],0.5)
label_data$colour_pvalue <- cut(label_data$'-log10p', breaks = c(-Inf, separate_num, Inf), labels = c(separate_num[1]-0.5, separate_num), right=FALSE)
fill_colour = c("olivedrab2") 
label_data <- table(label_data$group) %>% 
  as.data.frame() %>% 
  dplyr::rename(group=Var1) %>% 
  mutate(fill_colour = fill_colour ) %>% 
  left_join(label_data, ., "group") 



p1 <-
  ggplot(label_data)+

  geom_bar(aes(x=as.factor(id), y= 1 ), stat="identity", 
           alpha=0.2)  +

  geom_bar(aes(x=as.factor(id), y= RichFactor*18 , fill=group), stat="identity", 
           alpha=0.8)  + guides(fill=guide_legend(title=NULL)) + 

  geom_segment(data= label_data, aes(x = start, y = 1.42, xend =  end, yend = 1.42), 
               colour = label_data$fill_colour, alpha=1, linewidth=4 ,inherit.aes = FALSE) +  
  geom_segment(data= label_data, aes(x = start, y = 1.3, xend =  gene_length_end, yend = 1.3, colour = as.numeric(colour_pvalue)), alpha=1, size=3 ,inherit.aes = FALSE) + 
  guides(colour=guide_legend(title='-log10(pvalue)')) + 
  geom_segment(data= label_data, aes(x = start, y = 1.2, xend =  end, yend = 1.2),
               colour = "#8E96CC", alpha=1, size=3 ,inherit.aes = FALSE) +  
  geom_segment(data= label_data, aes(x = start, y = 1.2, xend =  up_down_gene_ratio_end, yend = 1.2),
               colour = "#740F67", alpha=1, size=3 ,inherit.aes = FALSE) +  
  geom_blank(aes(y = -0.5)) +
  
  theme_minimal() +
  theme(#legend.position = "none", 
    axis.text = element_blank(),
    axis.title = element_blank(), 
    panel.grid = element_blank(), 
    # plot.margin = unit(rep(-1,5), "cm") 
  )+ 
  coord_polar(theta = "x", start = 0, direction=1) +
  scale_fill_manual(values = fill_colour) +
  scale_color_steps(low = "#FEE8DE", high = "red", breaks = c(-Inf, separate_num, Inf))
p1



p2 <- p1+  geom_text(data=label_data, 
                 aes(x=(start+end)/2, y=1.42, label=ID, hjust=0.5),
                 color="black",fontface="bold",alpha=0.8, size=2.5, 
                 angle= label_data$angle, inherit.aes = T) + 
  geom_text(data=label_data, 
            aes(x=(start+gene_length_end)/2, y=1.3, label=all, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) +
  geom_text(data=label_data, 
            aes(x=(start+up_down_gene_ratio_end)/2, y=1.1, label=up_gene, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) +
  geom_text(data=label_data, 
            aes(x=(up_down_gene_ratio_end+end)/2, y=1.1, label=down_gene, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) 
p2

grid_data <- label_data[,c('ID', 'end', 'start','hjust'), drop=F]
grid_data <- grid_data %>% 
  mutate(distance = end - start) %>% 
  mutate(sec_breakpoint = start + (2/7)*distance) %>% 
  mutate(third_breakpoint = start + (4/7)*distance) %>% 
  mutate(fourth_breakpoint = start + (6/7)*distance) 
grid_ylim <- 1.47
grid_ylimax <- grid_ylim + 0.05
p3 <- p2 + 
  geom_segment(data= grid_data, aes(x = start, y = grid_ylim, xend =  start, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = sec_breakpoint, y = grid_ylim, xend =  sec_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = third_breakpoint, y = grid_ylim, xend =  third_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = fourth_breakpoint, y = grid_ylim, xend =  fourth_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE)   
# geom_segment(data= grid_data, aes(x = start, y = grid_ylim, xend =  fourth_breakpoint, yend = grid_ylim), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) 
p3


grid_data <- grid_data %>% 
  mutate(start_text = '1') %>% 
  mutate(sec_breakpoint_text = '10') %>% 
  mutate(third_breakpoint_text = '100') %>% 
  mutate(fourth_breakpoint_text = '1000') 
grid_ylimax_text <- grid_ylimax + 0.05
text_factor <- c('start_text','sec_breakpoint_text','third_breakpoint_text','fourth_breakpoint_text')
p4 <-
  p3 +
  geom_text(data=grid_data, 
            aes(x=start, y=grid_ylimax_text, label=start_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T) +
  geom_text(data=grid_data, 
            aes(x=sec_breakpoint, y=grid_ylimax_text, label=sec_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T)  +
  geom_text(data=grid_data, 
            aes(x=third_breakpoint, y=grid_ylimax_text, label=third_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T)  +
  geom_text(data=grid_data, 
            aes(x=fourth_breakpoint, y=grid_ylimax_text, label=fourth_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T) 
p4
ggsave("p3.svg",p4)




######################Fig.4:  sheet4################
enrich_circle_data<-read.xlsx("All_GOanalysis.xlsx", sheet=4,check.names = F, sep = "\t")
View(enrich_circle_data)

dat <- enrich_circle_data #[,c(1:4)]
colnames(dat)
colnames(dat)[2] <- 'group'
dat$'-log10p' <- -log10(dat$pvalue)

dat <- dat %>% filter(row_number() <= 25)


dat$RichFactor <- (dat$up + dat$down) / dat$all
dat$group <- as.factor(dat$group)
table(dat$group)

dat <- dat %>% dplyr::group_by(group)  %>% arrange(desc(RichFactor)) %>% arrange(group) 
dat$id <- seq(1, nrow(dat))

label_data <- dat#[,c('ID', 'id')]
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) / number_of_bar 
label_data$hjust <- ifelse(angle < -90 , 1, 0) 
label_data$angle <- ifelse(angle > 0 | angle < -180 , angle-90, angle+90) 
label_data$angle_vertical <- ifelse(angle < -90 , angle+180, angle) 
label_data$start <- label_data$id - 0.45
label_data <- label_data %>% 
  mutate(end=start + 0.9, all_gene=up_gene+down_gene) %>% 
  # mutate(deg_background_gene_ratio = (up_gene/all_gene)) %>% 
  mutate(gene_length = log10(all)) %>% 
  mutate(up_down_gene_ratio_end = start + (0.9*up_gene/all_gene), gene_length_end = start + 0.9 * (6/7) *(gene_length/3))  



range_sep_num <- range(label_data$'-log10p') %>% round(0)
separate_num <- seq(range_sep_num[1],range_sep_num[2],0.5)
label_data$colour_pvalue <- cut(label_data$'-log10p', breaks = c(-Inf, separate_num, Inf), labels = c(separate_num[1]-0.5, separate_num), right=FALSE)
fill_colour = c("#F7D116", "deepskyblue","olivedrab2") 
label_data <- table(label_data$group) %>% 
  as.data.frame() %>% 
  dplyr::rename(group=Var1) %>% 
  mutate(fill_colour = fill_colour ) %>% 
  left_join(label_data, ., "group") 


p1 <-
  ggplot(label_data)+

  geom_bar(aes(x=as.factor(id), y= 1 ), stat="identity", 
           alpha=0.2)  +

  geom_bar(aes(x=as.factor(id), y= RichFactor*13 , fill=group), stat="identity", 
           alpha=0.8)  + guides(fill=guide_legend(title=NULL)) +

  geom_segment(data= label_data, aes(x = start, y = 1.42, xend =  end, yend = 1.42), 
               colour = label_data$fill_colour, alpha=1, linewidth=4 ,inherit.aes = FALSE) +  
  geom_segment(data= label_data, aes(x = start, y = 1.3, xend =  gene_length_end, yend = 1.3, colour = as.numeric(colour_pvalue)), alpha=1, size=3 ,inherit.aes = FALSE) + 
  guides(colour=guide_legend(title='-log10(pvalue)')) + 
  geom_segment(data= label_data, aes(x = start, y = 1.2, xend =  end, yend = 1.2),
               colour = "#8E96CC", alpha=1, size=3 ,inherit.aes = FALSE) +  
  geom_segment(data= label_data, aes(x = start, y = 1.2, xend =  up_down_gene_ratio_end, yend = 1.2),
               colour = "#740F67", alpha=1, size=3 ,inherit.aes = FALSE) +  
  geom_blank(aes(y = -0.5)) +
 
  theme_minimal() +
  theme(#legend.position = "none", 
    axis.text = element_blank(),
    axis.title = element_blank(), 
    panel.grid = element_blank(), 
    # plot.margin = unit(rep(-1,5), "cm") 
  )+ 
  coord_polar(theta = "x", start = 0, direction=1) +
  scale_fill_manual(values = fill_colour) +
  scale_color_steps(low = "#FEE8DE", high = "red", breaks = c(-Inf, separate_num, Inf))
p1



p2 <- p1+  geom_text(data=label_data, 
                 aes(x=(start+end)/2, y=1.42, label=ID, hjust=0.5),
                 color="black",fontface="bold",alpha=0.8, size=2.5, 
                 angle= label_data$angle, inherit.aes = T) + 
  geom_text(data=label_data, 
            aes(x=(start+gene_length_end)/2, y=1.3, label=all, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) +
  geom_text(data=label_data, 
            aes(x=(start+up_down_gene_ratio_end)/2, y=1.1, label=up_gene, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) +
  geom_text(data=label_data, 
            aes(x=(up_down_gene_ratio_end+end)/2, y=1.1, label=down_gene, hjust=0.5),
            color="black",fontface="bold",alpha=0.8, size=1.8, 
            angle= label_data$angle, inherit.aes = FALSE) 
p2

grid_data <- label_data[,c('ID', 'end', 'start','hjust'), drop=F]
grid_data <- grid_data %>% 
  mutate(distance = end - start) %>% 
  mutate(sec_breakpoint = start + (2/7)*distance) %>% 
  mutate(third_breakpoint = start + (4/7)*distance) %>% 
  mutate(fourth_breakpoint = start + (6/7)*distance) 
grid_ylim <- 1.47
grid_ylimax <- grid_ylim + 0.05
p3 <- p2 + 
  geom_segment(data= grid_data, aes(x = start, y = grid_ylim, xend =  start, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = sec_breakpoint, y = grid_ylim, xend =  sec_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = third_breakpoint, y = grid_ylim, xend =  third_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) +  
  geom_segment(data= grid_data, aes(x = fourth_breakpoint, y = grid_ylim, xend =  fourth_breakpoint, yend = grid_ylimax), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE)   
# geom_segment(data= grid_data, aes(x = start, y = grid_ylim, xend =  fourth_breakpoint, yend = grid_ylim), colour = "black", alpha=0.8, size=0.5 ,inherit.aes = FALSE) 
p3


grid_data <- grid_data %>% 
  mutate(start_text = '1') %>% 
  mutate(sec_breakpoint_text = '10') %>% 
  mutate(third_breakpoint_text = '100') %>% 
  mutate(fourth_breakpoint_text = '1000') 
grid_ylimax_text <- grid_ylimax + 0.05
text_factor <- c('start_text','sec_breakpoint_text','third_breakpoint_text','fourth_breakpoint_text')
p4 <-
  p3 +
  geom_text(data=grid_data, 
            aes(x=start, y=grid_ylimax_text, label=start_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T) +
  geom_text(data=grid_data, 
            aes(x=sec_breakpoint, y=grid_ylimax_text, label=sec_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T)  +
  geom_text(data=grid_data, 
            aes(x=third_breakpoint, y=grid_ylimax_text, label=third_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T)  +
  geom_text(data=grid_data, 
            aes(x=fourth_breakpoint, y=grid_ylimax_text, label=fourth_breakpoint_text, hjust=hjust),
            color="black",fontface="bold",alpha=0.8, size=2.5, 
            angle= label_data$angle_vertical, inherit.aes = T) 
p4
ggsave("p4.svg",p4)



